#pragma once
#include <cstdarg>
#include <cstdio>
#include <ctime>
#include <string>

// 日志级别
#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define FATAL 4

const char* gLevelMap[] = {
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

#define LOGFILE "./log.txt"

void logMessage(int level, const char* format, ...)
{
    char stdbuffer[1024]; // 日志的标准部分
    time_t timestamp = time(nullptr);
    snprintf(stdbuffer, sizeof stdbuffer, "[%s] [%ld]", gLevelMap[level], timestamp);

    char logBuffer[1024];
    va_list args;
    va_start(args, format);
    vsnprintf(logBuffer, sizeof(logBuffer), format, args);
    va_end(args);

    // 把日志写入文件中
    FILE* fp = fopen(LOGFILE, "a");  // a是追加写
    fprintf(fp, "%s [%s:%d]| %s\n", stdbuffer, __FILE__, __LINE__,logBuffer);
    fclose(fp);
}

